package com.hnxxxy.workshop.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hnxxxy.workshop.mapper.ProductMapper;
import com.hnxxxy.workshop.model.DepotDO;
import com.hnxxxy.workshop.model.ProductDO;
import com.hnxxxy.workshop.request.ProductPageRequest;
import com.hnxxxy.workshop.request.ProductRequest;
import com.hnxxxy.workshop.service.ProductService;
import com.hnxxxy.workshop.vo.DepotVO;
import com.hnxxxy.workshop.vo.ProductVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 产品 服务实现类
 * </p>
 *
 * @author zqh
 * @since 2022-05-09
 */
@Service
@Slf4j
public class ProductServiceImpl  implements ProductService {

    @Resource
    private ProductMapper productMapper;

    @Override
    public void add(ProductRequest productRequest) {
        ProductDO productDO = new ProductDO();
        BeanUtils.copyProperties(productRequest, productDO);

        int rows = productMapper.insert(productDO);
        log.info("新增消息:rows={},data={}",rows,productDO);
    }

    @Override
    public void deleteById(int id) {
        int rows = productMapper.deleteById(id);

        log.info("删除消息条数 :rows={}",rows);
    }

    @Override
    public void update(ProductRequest productRequest) {
        ProductDO productDO = new ProductDO();
        BeanUtils.copyProperties(productRequest, productDO);

        int rows = productMapper.updateById(productDO);
        log.info("修改消息:rows={},data={}",rows,productDO);

    }

    @Override
    public ProductVO findByIdProduct(int id) {
        ProductVO productVO = new ProductVO();

        ProductDO productDO = productMapper.selectById(id);
        BeanUtils.copyProperties(productDO, productVO);
        log.info("根据id查询信息:id={}  productVO={}",id,productVO);
        return productVO;
    }

    @Override
    public Map<String, Object> page(ProductPageRequest productPageRequest) {
        int page = productPageRequest.getPage();
        int size = productPageRequest.getSize();

        String name = productPageRequest.getName();

        QueryWrapper<ProductDO> queryWrapper = new QueryWrapper<ProductDO>();

        if (!StrUtil.hasEmpty(name)){
            queryWrapper.like("name", name);
        }


        Page<ProductDO> pageInfo = new Page<>(page, size);

        IPage<ProductDO> productDOIPage = productMapper.selectPage(pageInfo,queryWrapper);

        Map<String,Object> pageMap = new HashMap<>(3);

        pageMap.put("total_record", productDOIPage.getTotal());
        pageMap.put("total_page", productDOIPage.getPages());
        pageMap.put("current_data",productDOIPage.getRecords()
                .stream().map(obj->beanProocess(obj)).collect(Collectors.toList()));

        log.info("分页消息:pageMap={}",pageMap);
        return pageMap;

    }

    private ProductVO beanProocess(ProductDO obj) {
        ProductVO productVO = new ProductVO();
        BeanUtils.copyProperties(obj, productVO);
        return productVO;
    }
}
